<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Basic Attribute Configuration</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Basic Attribute Configuration</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style type="text/css" scoped>
    .example-out .myclass-attrs {
        font-family:courier;
        margin-top:2px;
    }

    .example-out .myclass-title {
        font-weight:bold;
        font-family:arial;
        color:#8dd5e7;
        margin-top:5px;
        margin-bottom:3px;
    }

    .example-out {
        overflow:auto;
        border:1px solid #000;
        color:#ffffff;
        background-color:#004C6D;
        margin:5px;
        height:8em;
        padding:2px 2px 2px 5px;
    }
</style>

<div class="intro">
    <p>This example provides an introduction to the Attribute utility, showing how you can use it to add attribute support to your own custom classes.</p>
    <p>
    It is geared towards users who want to create their own classes from scratch and add Attribute support. In most cases you should consider extending the <a href="../base/index.html"><code>Base</code></a> class when you need managed attribute support, 
    instead of augmenting Attribute directly, especially if you expect your class to be extended. <a href="../base/index.html"><code>Base</code></a> does the work described in this example for you, in addition to making it easier for users to extend you class.
    </p>    
</div>

<div class="example">
    <div id="createo1">
    <button type="button" class="do">Create First Instance</button> Construct o1, with default attribute values
    <div class="example-out"></div>
</div>
<div id="updateo1">
    <button type="button" class="do">Update First Instance</button> Update the first instance, using set
    <div class="example-out"></div>
</div>
<div id="createo2">
    <button type="button" class="do">Create Second Instance</button> Create the second instance, passing initial values to the constructor
    <div class="example-out"></div>
</div>

<script type="text/javascript">

// Get a new instance of YUI and 
// load it with the required set of modules

YUI().use("node", "attribute", function(Y) {

    // Setup custom class which we want to 
    // add managed attribute support to

    function MyClass(cfg) {

        // When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
        var attrs = {
            // Add 3 attributes, foo, bar and foobar
            "foo" : {
                value:5
            },
     
            "bar" : {
                value:"Hello World!"
            },
    
            "foobar" : {
                value:true
            }
        };

        this.addAttrs(attrs, cfg);
    }

    // Augment custom class with Attribute
    Y.augment(MyClass, Y.Attribute);

    function displayValues(o, title, node) {
        var str = 
            '<div class="myclass"><div class="myclass-title">' 
            + title + 
            '</div><ul class="myclass-attrs"><li>foo: ' 
            + o.get("foo") 
            + '</li><li>bar: '
            + o.get("bar")
            + '</li><li>foobar: '
            + o.get("foobar")
            + '</li></ul></div>';

        Y.one(node).set("innerHTML", str);
    }

    Y.on("click", function() {

        // Create a new instance, but don't provide any initial attribute values.
        var o1 = new MyClass();

        // Display current values
        displayValues(o1, "o1 with default values, set during construction", "#createo1 .example-out");

        Y.on("click", function() {

            // Update values, using the "set" method
            o1.set("foo", 10);
            o1.set("bar", "Hello New World!");
            o1.set("foobar", false);

            displayValues(o1, "o1 values updated using set, after construction", "#updateo1 .example-out");

        }, "#updateo1 .do");

    }, "#createo1 .do");

    Y.on("click", function() {

        var o2 = new MyClass({
            foo: 7,
            bar: "Aloha World!",
            foobar: false
        });

        displayValues(o2, "o2 values set during construction", "#createo2 .example-out");

    }, "#createo2 .do");
});
</script>

</div>

<h2>Setting Up Your Own Class To Use Attribute</h2>

<p>In this example, we'll show how you can use the Attribute utility to add managed attributes to your own object classes. Later examples will show how you can configure more advanced attribute properties, and work with attribute change events.</p>

<h3>Creating A YUI Instance</h3>

<p>Before we get into attribute, a quick note on how we set up the instance of YUI we'll use for the examples. For all of the attribute examples, we'll setup our own instance of the YUI object and download the files we require on demand using the code pattern shown below:</p>

<pre class="code prettyprint">&lt;script type=&quot;text&#x2F;javascript&quot;&gt;

    &#x2F;&#x2F; Create our local YUI instance, to avoid
    &#x2F;&#x2F; modifying the global YUI object

    YUI({...}).use(&quot;attribute&quot;, &quot;node&quot;, function(Y) {

        &#x2F;&#x2F; Example code is written inside this function,
        &#x2F;&#x2F; which gets passed our own YUI instance, Y, loaded
        &#x2F;&#x2F; with the modules we asked for - &quot;attribute&quot; and &quot;node&quot;

    });
&lt;&#x2F;script&gt;</pre>


<p>The call to <code>YUI()</code> will create and return a new instance of the global YUI object for us to use. However this instance does not yet have all the modules we need for the examples.</p>

<p>To load the modules, we invoke <code>use()</code> and pass it the list of modules we'd like populated on our new YUI instance - in this case, <code>attribute</code> and <code>node</code>.

The YUI instance will pull down the source files for modules if they don't already exist on the page, plus any or their dependencies.
When the source files are done downloading, the callback function which we pass in as the 3rd argument to <code>use()</code>, is invoked. Our custom YUI instance, <code>Y</code>, is passed to the callback, populated with the classes which make up the requested modules.</p>

<p>This callback function is where we'll write all our example code. By working inside the callback function, we don't pollute the global namespace and we're also able to download the files we need on demand, rather than have them be on the page up front.</p>

<p>The configuration object passed to <code>YUI()</code> when creating the instance is used to specify how (<em>combined, separate, debug, min etc.</em>) we want the files downloaded, and from where. The API documentation for the <a href="http://yuilibrary.com/yui/docs/api/YUI.html">YUI object</a>, provides more information about the configuration options available.</p>

<h3>Defining Your Custom Class</h3>

<p>The first step in the example is to create the constructor function for our new class, to which we want to add attribute support. In our example, this class is called <code>MyClass</code>.

We then augment <code>MyClass</code> with <code>Y.Attribute</code>, so that it receives all of <code>Attribute&#x27;s</code> methods:</p>

<pre class="code prettyprint">function MyClass(cfg) {
    ...
}

Y.augment(MyClass, Y.Attribute);</pre>


<h3>Adding Attributes</h3>

<p>We can now set up any attributes we need for <code>MyClass</code> using the <code>addAttrs</code> method. For the basic example we add 3 attributes - <code>foo</code>,<code>bar</code>, and <code>foobar</code>, and provide an initial <code>value</code> for each. 

The same object literal we use to provide the initial value for the attribute will also be used in the other examples to configure attribute properties such as <code>readOnly</code> or <code>writeOnce</code>, and define <code>getter</code>, <code>setter</code> and <code>validator</code> methods for the attribute.</p>

<p>In this example, the default set of attributes which <code>MyClass</code> will support gets passed to <code>addAttrs</code> to set up the attributes for each instance during construction.</p>

The complete definition for <code>MyClass</code> is shown below:</p>

<pre class="code prettyprint">&#x2F;&#x2F; Setup custom class which we want to add managed attribute support to
function MyClass(cfg) {

    &#x2F;&#x2F; When constructed, setup the initial attributes for the 
    &#x2F;&#x2F; instance, by calling the addAttrs method.
    var attrs = {
        &#x2F;&#x2F; Add 3 attributes, foo, bar and foobar
        &quot;foo&quot; : {
            value:5
        },
 
        &quot;bar&quot; : {
            value:&quot;Hello World!&quot;
        },

        &quot;foobar&quot; : {
            value:true
        }
    };

    this.addAttrs(attrs, cfg);
}

&#x2F;&#x2F; Augment custom class with Attribute 
Y.augment(MyClass, Y.Attribute);</pre>


<p>The <code>addAttrs</code> method, in addition to the default attribute configuration, also accepts an object literal (associative array) of name/value pairs which can be used to over-ride the default initial values of the attributes. This is useful for classes which wish to allow the user to set the value of attributes as part of object construction, as shown by the use of the <code>cfg</code> argument above.</p>

<p>
As mentioned previously, if you expect your class to be extended, <a href="../base/index.html">Base</a> provides a more convenient way for you to define the same attribute configuration statically for your class, so that it can be easily modified by extended classes. Base will take care of isolating the static configuration, so that it isn't modified across instances. 
</p>

<h3>Using Attributes</h3>

<p>Now that we have <code>MyClass</code> defined with a set of attributes it supports, users can get and set attribute values on instances of <code>MyClass</code>:</p>

<p>We construct the first instance, <code>o1</code>, without setting any initial attribute values in the constructor, but use Attribute's <code>set()</code> method to set values after construction:</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new instance, but don&#x27;t provide any initial attribute values.
var o1 = new MyClass();

&#x2F;&#x2F; Display current values
displayValues(o1, &quot;o1 with default values, set during construction&quot;, 
                &quot;#createo1 .example-out&quot;);

...

&#x2F;&#x2F; Update values, using the &quot;set&quot; method
o1.set(&quot;foo&quot;, 10);
o1.set(&quot;bar&quot;, &quot;Hello New World!&quot;);
o1.set(&quot;foobar&quot;, false);

displayValues(o1, &quot;o1 values updated using set, after construction&quot;, 
                &quot;#updateo1 .example-out&quot;);</pre>


<p>For the second instance that, <code>o2</code> we set the initial values of the attributes, using the constructor configuration argument:</p>

<pre class="code prettyprint">var o2 = new MyClass({
    foo: 7,
    bar: &quot;Aloha World!&quot;,
    foobar: false
});</pre>


<p>The <code>displayValues()</code> method uses Attribute's <code>get()</code> method to retrieve the current values of the attributes, to display:</p>

<pre class="code prettyprint">function displayValues(o, title, node) {
    var str = 
        &#x27;&lt;div class=&quot;myclass&quot;&gt;&lt;div class=&quot;myclass-title&quot;&gt;&#x27; 
        + title + 
        &#x27;:&lt;&#x2F;div&gt;&lt;ul class=&quot;myclass-attrs&quot;&gt;&lt;li&gt;foo:&#x27; 
        + o.get(&quot;foo&quot;) 
        + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;bar:&#x27;
        + o.get(&quot;bar&quot;)
        + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;foobar:&#x27;
        + o.get(&quot;foobar&quot;)
        + &#x27;&lt;&#x2F;li&gt;&lt;&#x2F;ul&gt;&lt;&#x2F;div&gt;&#x27;;

    &#x2F;&#x2F; Use the Y.one() method to get the first element which 
    &#x2F;&#x2F; matches the selector passed in, to output the string to...       
    Y.one(node).set(&quot;innerHTML&quot;, str);
}</pre>


<h2>Complete Example Source</h2>

<pre class="code prettyprint">&lt;div id=&quot;createo1&quot;&gt;
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Create First Instance&lt;&#x2F;button&gt; Construct o1, with default attribute values
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;updateo1&quot;&gt;
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Update First Instance&lt;&#x2F;button&gt; Update the first instance, using set
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;
&lt;div id=&quot;createo2&quot;&gt;
    &lt;button type=&quot;button&quot; class=&quot;do&quot;&gt;Create Second Instance&lt;&#x2F;button&gt; Create the second instance, passing initial values to the constructor
    &lt;div class=&quot;example-out&quot;&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;

&#x2F;&#x2F; Get a new instance of YUI and 
&#x2F;&#x2F; load it with the required set of modules

YUI().use(&quot;node&quot;, &quot;attribute&quot;, function(Y) {

    &#x2F;&#x2F; Setup custom class which we want to 
    &#x2F;&#x2F; add managed attribute support to

    function MyClass(cfg) {

        &#x2F;&#x2F; When constructed, setup the initial attributes for the instance, by calling the addAttrs method.
        var attrs = {
            &#x2F;&#x2F; Add 3 attributes, foo, bar and foobar
            &quot;foo&quot; : {
                value:5
            },
     
            &quot;bar&quot; : {
                value:&quot;Hello World!&quot;
            },
    
            &quot;foobar&quot; : {
                value:true
            }
        };

        this.addAttrs(attrs, cfg);
    }

    &#x2F;&#x2F; Augment custom class with Attribute
    Y.augment(MyClass, Y.Attribute);

    function displayValues(o, title, node) {
        var str = 
            &#x27;&lt;div class=&quot;myclass&quot;&gt;&lt;div class=&quot;myclass-title&quot;&gt;&#x27; 
            + title + 
            &#x27;&lt;&#x2F;div&gt;&lt;ul class=&quot;myclass-attrs&quot;&gt;&lt;li&gt;foo: &#x27; 
            + o.get(&quot;foo&quot;) 
            + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;bar: &#x27;
            + o.get(&quot;bar&quot;)
            + &#x27;&lt;&#x2F;li&gt;&lt;li&gt;foobar: &#x27;
            + o.get(&quot;foobar&quot;)
            + &#x27;&lt;&#x2F;li&gt;&lt;&#x2F;ul&gt;&lt;&#x2F;div&gt;&#x27;;

        Y.one(node).set(&quot;innerHTML&quot;, str);
    }

    Y.on(&quot;click&quot;, function() {

        &#x2F;&#x2F; Create a new instance, but don&#x27;t provide any initial attribute values.
        var o1 = new MyClass();

        &#x2F;&#x2F; Display current values
        displayValues(o1, &quot;o1 with default values, set during construction&quot;, &quot;#createo1 .example-out&quot;);

        Y.on(&quot;click&quot;, function() {

            &#x2F;&#x2F; Update values, using the &quot;set&quot; method
            o1.set(&quot;foo&quot;, 10);
            o1.set(&quot;bar&quot;, &quot;Hello New World!&quot;);
            o1.set(&quot;foobar&quot;, false);

            displayValues(o1, &quot;o1 values updated using set, after construction&quot;, &quot;#updateo1 .example-out&quot;);

        }, &quot;#updateo1 .do&quot;);

    }, &quot;#createo1 .do&quot;);

    Y.on(&quot;click&quot;, function() {

        var o2 = new MyClass({
            foo: 7,
            bar: &quot;Aloha World!&quot;,
            foobar: false
        });

        displayValues(o2, &quot;o2 values set during construction&quot;, &quot;#createo2 .example-out&quot;);

    }, &quot;#createo2 .do&quot;);
});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Use the Attribute API to define, set and get attribute values.">
                                        <a href="attribute-basic.html">Basic Attribute Configuration</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Configure attributes to be readOnly or writeOnce.">
                                        <a href="attribute-rw.html">Read-Only and Write-Once Attributes</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to listen for changes in attribute values.">
                                        <a href="attribute-event.html">Attribute Change Events</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Create a basic SpeedDater class, with Attribute support.">
                                        <a href="attribute-basic-speeddate.html">Attribute Based Speed Dating</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Refactors the basic Speed Dating example, to use attribute change events to update rendered elements, and have two instances react to another.">
                                        <a href="attribute-event-speeddate.html">Attribute Event Based Speed Dating</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Add custom methods to get and set attribute values and provide validation support.">
                                        <a href="attribute-getset.html">Attribute Getters, Setters and Validators</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
